var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var timerButton = document.getElementById('timer');
var messageButton = document.getElementById('message');
var counter = 1;
/*
  
  class Promise<T>(cb)



  class Promise<T>(cb: (complete, fail) => void)
  
                     complete: (value: T) => void
                     fail: (error: Error) => void


  class Promise<T>(cb: (complete: (value: T) => void, fail: (error: Error) => void) => void)

  
  

  松散参数类型


  class Promise<T>(cb: (complete, fail) => void) {
    then<V>(cb: (value: T), error: Error): Promise<V>
    catch(error: Error)
  }


  class Promise(cb) {
    then(cb, error)
    catch(cb)
  }
*/
function changePromise() {
    return new Promise(function (complete, fail) {
        setTimeout(function () {
            timerButton.innerHTML = String(counter);
            counter++;
            complete();
        }, 1000);
    });
}
function changeAsync() {
    return __awaiter(this, void 0, void 0, function* () {
        yield changePromise();
        yield changePromise();
        yield changePromise();
        yield changePromise();
        yield changePromise();
        yield changePromise();
        messageButton.innerHTML = 'Promise Async finished';
    });
}
//changeAsync()
function throwError() {
    throw new Error('An error raised');
}
function changeAsyncWithError() {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            yield changePromise();
            yield changePromise();
            yield changePromise();
            throwError();
            yield changePromise();
            yield changePromise();
            yield changePromise();
            messageButton.innerHTML = 'Promise Async finished';
        }
        catch (e) {
            messageButton.innerHTML = e.message;
        }
    });
}
changeAsyncWithError();
// changePromise()
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     messageButton.innerHTML = 'Promise2 finished'
//   }) 
